﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gedea.WinForm.Helpers;
using System.Runtime.InteropServices;
using System.Threading;

namespace Gedea.WinForm
{
    public partial class Waiting : Form
    {
        Thread t;

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect, // x-coordinate of upper-left corner
             int nTopRect, // y-coordinate of upper-left corner
             int nRightRect, // x-coordinate of lower-right corner
             int nBottomRect, // y-coordinate of lower-right corner
             int nWidthEllipse, // height of ellipse
             int nHeightEllipse // width of ellipse
         );

        public Waiting()
        {
            InitializeComponent();
            //this.ClientSize = this.BackgroundImage.Size;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.StartPosition = FormStartPosition.CenterScreen;
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width - 10, Height - 10, 20, 20)); // adjust these parameters to get the lookyou want.

            this.Shown += new EventHandler(Waiting_Shown);
            this.FormClosing += new FormClosingEventHandler(Waiting_FormClosing);

        }

        public void Waiting_Shown(object sender, EventArgs e)
        {
            initValues();
            t = new System.Threading.Thread(initApplication);
            t.Start();
        }

        void Waiting_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (t != null && t.IsAlive)
            {
                t.Abort();
                t = null;
            }
        }

        public void initValues()
        {
            //titleLabel.Text = String.Format("{0} Demo", Application.ProductName);
            label1.Text = "Initializing...";
        }

        public void initApplication()
        {
            List<string> msgList = new List<string>() 
            {
                "Searching for updates...", 
                "Connectiong to database...",
                "Connectiong to webservices...",
                "Loading settings...",
                "Loading user preferences...",
                "Starting application..."
            };
            foreach (string msg in msgList)
            {
                Thread.Sleep(500);
                this.Invoke((MethodInvoker)(() => setMessage(msg)));
            }
            if (this.InvokeRequired) this.Invoke(new Action(finishProcess));
        }

        public void setMessage(string msg)
        {
            label1.Text = msg;
        }

        private void finishProcess()
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
    }
}
